package com.example.girl.activity;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.girl.http.APIRetrofit;
import com.example.girl.http.APIService;
import com.example.girl.http.Return;
import com.example.lbstest.R;
import com.github.czy1121.view.RoundButton;
import com.kaopiz.kprogresshud.KProgressHUD;
import com.king.base.util.LogUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * 注册界面
 */
public class XGMM_Activity extends Activity {

    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.textView22)
    TextView textView22;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.textView23)
    TextView textView23;
    @BindView(R.id.et_mm)
    EditText etMm;
    @BindView(R.id.bt_getCode)
    RoundButton btGetCode;
    @BindView(R.id.activity_main)
    LinearLayout activityMain;

    @BindView(R.id.et_qrmm)
    EditText etQrmm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhuces_);
        ButterKnife.bind(this);


    }
    KProgressHUD dialog;

    public void showdialog() {
        dialog = KProgressHUD.create(XGMM_Activity.this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setLabel("正在登录中，请稍候")
                .setCancellable(true)
                .setAnimationSpeed(2)
                .setDimAmount(0.5f)
                .show();
    }
    @OnClick({R.id.img_back, R.id.bt_getCode})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.bt_getCode:
                if (TextUtils.isEmpty(etPhone.getText().toString())) {
                    Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(etMm.getText().toString())) {
                    Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (etMm.getText().toString().length() < 6 || etQrmm.getText().toString().length() < 6) {
                    Toast.makeText(this, "密码太短，请重新输入！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!etMm.getText().toString().equals(etQrmm.getText().toString())) {
                    Toast.makeText(this, "两次输入的密码不同，请重新输入！", Toast.LENGTH_SHORT).show();
                    return;
                }
                showdialog();
                APIRetrofit.getInstance()
                        .create(APIService.class)
                        .register(etPhone.getText().toString(), etMm.getText().toString(),

                                "无", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1")
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<Return>() {
                            @Override
                            public void onCompleted() {
                                LogUtils.d("onCompleted");

                            }

                            @Override
                            public void onError(Throwable e) {
                                LogUtils.e(e);
                                if (dialog != null) {
                                    dialog.dismiss();
                                }
                                Toast.makeText(XGMM_Activity.this, "" + e, Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onNext(Return girlResult) {
                                LogUtils.d(girlResult.toString());
                                if (dialog != null) {
                                    dialog.dismiss();
                                }
                                if (girlResult.getCode() == 10001) {
                                    Toast.makeText(XGMM_Activity.this, "账号注册成功！", Toast.LENGTH_SHORT).show();

                                    finish();
                                }

                            }
                        });
//                API.Updatepassword(etPhone.getText().toString(), etMm.getText().toString(), new Okhttp.Objectcallback() {
//                    @Override
//                    public void onsuccess(String st) {
//                        XGMM_Bean login_bean = JSONUtils.parseJSON(st, XGMM_Bean.class);
//                        Log.e("777", st);
//                        if (login_bean.getCode() .equals("10001" ) ) {
//                            Toast.makeText(XGMM_Activity.this, "方法调用错误", Toast.LENGTH_SHORT).show();
//                        } else if (login_bean.getCode() .equals("10004" ) ){
//                            Toast.makeText(XGMM_Activity.this, "密码错误", Toast.LENGTH_SHORT).show();
//                        } else if (login_bean.getCode() .equals("10005" )) {
//                            Toast.makeText(XGMM_Activity.this, "账号不存在", Toast.LENGTH_SHORT).show();
//                        } else if (login_bean.getCode() .equals("10003" )) {
//                            Toast.makeText(XGMM_Activity.this, "服务器异常", Toast.LENGTH_SHORT).show();
//                        } else if (login_bean.getCode() .equals("10000" )) {
//                            Toast.makeText(XGMM_Activity.this, "密码修改成功！", Toast.LENGTH_SHORT).show();
//
//                             finish();
//                        }
//                    }
//
//                    @Override
//                    public void onFalia(int code, String errst) {
//                        Toast.makeText(XGMM_Activity.this, "网络错误，请检查网络！", Toast.LENGTH_SHORT).show();
//                    }
//                });
//                Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
//                finish();


                break;
        }
    }
}
